#!/bin/bash

mod_name=$1
sys_root=$2
if [[ "${sys_root:0-1}" == "/" ]]; then
    sys_root=${sys_root%?}
fi

function getMod(){
	mod_path=$(modinfo $1 | grep filename | sed 's/^filename:\s*//g')
    mod_dir=$(dirname $mod_path)
	depends=$(modinfo $1 | grep depends | sed 's/depends:\s*//g' | awk '{split($0,arr,",");for(i in arr) print arr[i]}')
	mod_name=$(basename $mod_path)
	if [ ! -e ${sys_root}${mod_path} ];
    then
		cp --parents ${mod_path} ${sys_root}
		echo -e "\033[32mCopy ${1} success.\033[0m"
		for ed in $depends;
        do
			getMod $ed
		done
	fi
}


if modinfo $mod_name  &> /dev/null;
then
	getMod $mod_name
else
	echo "No such module"
fi
